package com.logistica.metropolitana.web.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.logistica.metropolitana.common.web.BaseController;
import com.logistica.metropolitana.exceptions.UsuarioException;
import com.logistica.metropolitana.services.UsuarioService;
import com.logistica.metropolitana.web.views.PermisoView;
import com.logistica.metropolitana.web.views.UsuarioView;

@Controller
public class UsuarioController extends BaseController {

	private final Logger logger = LoggerFactory.getLogger(UsuarioController.class);	
	
	@Autowired
	UsuarioService usuarioService;
	
	@RequestMapping(value = "ajax/usuarios", method = RequestMethod.GET)
	public String goUsuarios(Model model, HttpSession httpSession) throws UsuarioException {		
		try {
			logger.info("Mostrar lista de usuarios");
			model.addAttribute("usuarios", usuarioService.getUsuarios());
		}catch(UsuarioException uException){
			throw uException;
		}catch(Exception e){
			UsuarioException uException = new UsuarioException(e, 
					UsuarioException.LAYER_CONVERTER, UsuarioException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, uException);
			throw uException;
		}
		return "usuarios";
	}
	
	@RequestMapping(value = "ajax/{username}/editar_usuario", method = RequestMethod.GET)
	public String editUsuario(@PathVariable("username") String username, Model model, HttpSession httpSession) throws UsuarioException {		
		try {
			logger.info("Usuario a Editar {}", username);
			
			UsuarioView usuarioView = usuarioService.getUsuarioByUsername(username);
			Map<String, List<PermisoView>> usuarioPermisos = usuarioService.getUsuarioPermisos(username);
			
			model.addAttribute("usuarioView", usuarioView);			
			model.addAttribute("usuarioPermisos", usuarioPermisos);
		}catch(UsuarioException uException){
			throw uException;
		}catch(Exception e){
			UsuarioException uException = new UsuarioException(e, 
					UsuarioException.LAYER_CONVERTER, UsuarioException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, uException);
			throw uException;
		}
		return "editar_usuario";
	}
	
	@RequestMapping(value = "ajax/{username}/editar_usuario", method = RequestMethod.POST)
	public String postUsuario(@PathVariable("username") String username, Model model, @ModelAttribute("usuarioView") 
					UsuarioView usuarioView, HttpSession httpSession) throws UsuarioException {		
		try {
			
			logger.info("Usuario a Editar {}", username);
			
			model.addAttribute("usuarioView", usuarioView);
			
			usuarioService.addPermisos(usuarioView);
			
			httpSession.setAttribute(SUCCESS_MESSAGE, "El usuario " + usuarioView.getNombre() + " ha sido actualizado correctamente");

		}catch(UsuarioException uException){
			throw uException;
		}catch(Exception e){
			UsuarioException uException = new UsuarioException(e, 
					UsuarioException.LAYER_CONTROLLER, UsuarioException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, uException);
			throw uException;
		}
		return "redirect:/home.html#ajax/usuarios.html";
	}
	
}
